#! /usr/bin/python

import os
import sys

cmd = ""
dbName = "ssb"
username = "monetdb"
password = "monetdb"

inputPath="."
outputPath = "/tmp/ssb"
dbFarm = "/home/yuanyuan/monetdb/ssb/"

if not os.path.exists(inputPath):
    exit(-1)

files = os.listdir(inputPath)
files.sort()

os.system("echo > ./.monetdb");
cmd = "echo user=" + username + " >>./.monetdb"
os.system(cmd)

cmd = "echo password=" + password + " >>./.monetdb"
os.system(cmd)

for file in files: 
    nameA = file.split(".sql")
    if len(nameA) !=2:
        continue

    if not os.path.exists(outputPath):
        os.makedirs(outputPath)

    outFile = outputPath + "/" + nameA[0] 

    cmd = "monetdbd stop " + dbFarm
    os.system(cmd)

    cmd = "sleep 8"
    os.system(cmd)

    cmd = "monetdbd start " + dbFarm
    os.system(cmd)

    cmd = "echo 1 > /proc/sys/vm/drop_caches"
    os.system(cmd)

    cmd = "mclient -d " + dbName + " -ims < "  + inputPath + "/" + file

    os.system(cmd)

    cmd += " > " + outFile
    os.system(cmd)

files = os.listdir(outputPath)
files.sort()

for file in files:
    fpath = outputPath + "/" + file
    fd = open(fpath, "r")
    lines = fd.readlines()
    for line in lines:
        t = line.split("(")
        if len(t) != 2:
            continue

        t1 = t[1].split("ms)")
        print t1[0]

    fd.close()

os.system("rm -rf ./.monetdb")

